// XMLHTTPRequest Enable
function createObject() {
	var request_type;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		
	}else{
		request_type = new XMLHttpRequest();
	}
	return request_type;
}

var http = createObject();


//LOGIN
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */
var nocache = 0;

function login() {
	// Show a waiting message in the layer with ID ajax_response
	document.getElementById('login_response').innerHTML = "Loading..."
	// verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
	var myusername = encodeURI(document.getElementById('myusername').value);
	var mypassword = encodeURI(document.getElementById('mypassword').value);
	// Set te random number to add to URL request
	nocache = Math.random();
	// Pass the login variables like URL variable
	http.open('get', 'login.php?myusername='+myusername+'&mypassword='+mypassword+'&nocache = '+nocache);
	http.onreadystatechange = loginReply;
	http.send(null);
}

function loginReply() {
	if(http.readyState == 4){ 
		var response = http.responseText;
		if(response == 0){ // if login fails
			document.getElementById('login_response').innerHTML = 'Login failed! Verify user and password';
		} else { //if login ok redirect to profile
		//document.getElementById('login_response').innerHTML = 'Welcome, '+response+'!';
		window.location='dashboard.php';
		}
	}
}